﻿using FactoryDeviceSystem.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryDeviceSystem.Controls
{
    public partial class UPanel : Panel
    {
        Rectangle rect;
        public UPanel()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint,true);
            SetStyle(ControlStyles.UserPaint,true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.BackColor = Color.Transparent;
            this.rect = this.ClientRectangle;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.rect = this.ClientRectangle;
            this.Region = new Region(rect);
            this.rect.Width -= 1;
            this.rect.Height -= 1;
        }

        private float radius = 5;
        [Description("按钮圆角大小")]
        public float Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                Invalidate();
            }
        }

        private float borderWidth = 0;
        [Description("按钮边框大小")]
        public float BorderWidth
        {
            get { return borderWidth; }
            set
            {
                borderWidth = value;
                Invalidate();
            }
        }

        private Color borderColor = Color.Gray;
        [Description("按钮边框颜色")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                Invalidate();
            }
        }


        private Color bgColor1 = Color.Gray;
        [Description("渐变背景色1")]
        public Color BgColor1
        {
            get { return bgColor1; }
            set
            {
                bgColor1 = value;
                Invalidate();
            }
        }

        private Color bgColor2 = Color.Silver;
        [Description("渐变背景色2")]
        public Color BgColor2
        {
            get { return bgColor2; }
            set
            {
                bgColor2 = value;
                Invalidate();
            }
        }

        private LinearGradientMode gradientMode = LinearGradientMode.Horizontal;
        [Description("渐变方向")]
        public LinearGradientMode GradientMode
        {
            get { return gradientMode; }
            set
            {
                gradientMode = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;

            // 边框、填充
            Rectangle rect1 = rect;
            Rectangle rect2;
            GraphicsPath path1;
            GraphicsPath path2;

            LinearGradientBrush brush = new LinearGradientBrush(rect1, bgColor1, bgColor2, GradientMode);

            if (radius > 0)
            {
                if (borderWidth > 0)
                {
                    rect2 = new Rectangle((int)(rect1.X + borderWidth), (int)(rect1.Y + borderWidth), (int)(rect1.Width - 2 * borderWidth), (int)(rect1.Height - 2 * borderWidth));
                    path1 = DrawUtil.GetRoundPath(rect2, radius);
                }
                else
                {
                    path1 = DrawUtil.GetRoundPath(rect1, radius);
                }
                path2 = DrawUtil.GetRoundPath(rect1, radius);
                g.FillPath(brush, path1);
                g.DrawPath(new Pen(borderColor, borderWidth), path1);
            }
            else
            {
                if (borderWidth > 0)
                {
                    rect2 = new Rectangle((int)(rect1.X + borderWidth), (int)(rect1.Y + borderWidth), (int)(rect1.Width - 2 * borderWidth), (int)(rect1.Height - 2 * borderWidth));
                }
                else
                {
                    rect2 = rect1;
                }
                g.FillRectangle(brush, rect1);
                g.DrawRectangle(new Pen(borderColor, borderWidth), rect2);
            }
        }
    }
}
